<?php

namespace Modules\System\Services\Mds;

class MDSArray
{

    /**
     * 根据一个一维数组中的一些值，来筛选二维数组中某个字段包含这个值的数据。
     * @param direction
     */
    static public function getArrayByOne($arr = [],$data = [],$field = ''){
        $newData = [];
        foreach($arr as $k => $v){
            if($v == "*"){
                return $data;
            }
            if($v == "MDSLine"){
                $newData[] = (object)['field'=>'MDSLine'];                    //标记自己独特间隔用
                continue;
            }
            foreach($data as $kd => $vd){
                if($data[$kd][$field] == $v){
                    $newData[$kd] = $vd;
                }
            }
        }
        return $newData;
    }

    /**
     * 根据一个一维数组中的一些值，来删除二维数组中某个字段包含这个值的数据。
     * @param direction
     */
    static public function deleteArrayField($data = [],$fieldArr = []){
        if(isset($data[0])){
            foreach($data as $k => $v){
                foreach($fieldArr as $kv => $vv){
                    unset($data[$k][$vv]);
                }
            }
        }else{
            foreach($fieldArr as $kv => $vv){
                unset($data[$vv]);
            }
        }
        return $data;
    }

    /**
     * 用数组中的其中两个值作为新数组的键和值
     * @param direction
     */
    static public function arraykeyVal($data = [],$key,$val){

        $dataNew = [];
        foreach($data as $k=>$v){
            $dataNew[$v[$key]] = $v[$val];
        }
        return $dataNew;

    }

    /**
     * json变成数组
     * @param direction
     */
    static public function jsonToArray($data){
        if(gettype($data) == "array"){
            return $data;
        }
        return json_decode($data,true);
    }

    /**
     * 字符串变成数组
     * @param direction
     */
    static public function strToArray($data,$Separator){
        if(gettype($data) == "array"){
            return $data;
        }
        return explode($Separator,$data);
    }

    /**
     * 获取菜单格式数据
     * @param direction
     */
    static public function menuTreeArr($AuthRole){

        $newData = [];
        foreach($AuthRole as $k => $v){
            if($v['grade'] == 1){
                $one = [];
                foreach($AuthRole as $kt => $vt){
                    if($vt['group_id'] == $v['controller_id']){
                        $one[] = $vt;
                    }
                }
                $v['child'] = $one;
                $newData[] = $v;
            }
        }
        return $newData;

    }

    /**
     * 对象变数组
     * @param direction
     */
    static public function objectToArray($obj){
        if(!empty($obj)){
            return json_decode(json_encode($obj),true);
        }else{
            return [];
        }
    }

    /**
     * 二维数组删除其中的null
     */
    static public function delNull($arr){
        foreach($arr as $k=>$v){
            if(gettype($v) == 'array'){
                foreach($v as $k2=>$v2){
                    if($v2 == null){
                        $arr[$k][$k2] == '';
                    }
                }
            }else{
                if($v == null){
                    $arr[$k] == '';
                }
            }
        }
        return $arr;
    }

}